package hello;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @author Mark
 * @date 2020/5/25 17:52
 * @description 这是第一个servlet
 */
public class HelloServlet extends HttpServlet {

    // 理论上是一致(doGet()和doPost())
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 输出(编写映射)
        PrintWriter writer = resp.getWriter();
        writer.print("Hello,Servlet");

        // 中间商(共享数据-保存数据)
        ServletContext servletContext = this.getServletContext();
        System.out.println("servletContext = " + servletContext);
        String userName = "Wang";
        servletContext.setAttribute("userName",userName);
        servletContext.getAttribute(userName);
        Enumeration<String> initParameterNames = this.getInitParameterNames();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
